package com.atguigu.sparkcore.day02.kv

import org.apache.spark.{SparkConf, SparkContext}

/**
 * Author atguigu
 * Date 2020/10/28 11:31
 */
object FoldByKey {
    def main(args: Array[String]): Unit = {
        //1.创建SparkConf并设置App名称
        val conf: SparkConf = new SparkConf().setAppName("SparkCoreTest").setMaster("local[*]")
        
        //2.创建SparkContext，该对象是提交Spark App的入口
        val sc: SparkContext = new SparkContext(conf)
        
        //3具体业务逻辑
        //3.1 创建第一个RDD
        val rdd = sc.makeRDD(List(("a", 1), ("b", 5), ("a", 2), ("a", 5),("a", 5), ("b", 2)), 2)
        // 零值: 只在分区内聚合参与, 分区间不参与
        val rdd2 = rdd.foldByKey(0)(_ + _)
        rdd2.collect.foreach(println)
        
        sc.stop()
        
    }
}
